﻿using System.Collections.Generic;
using Azure2Java.QPCRAdapter.Adapter;
using Azure2Java.QPCRAdapter.Adapter.Entity;
using Azure.QPCR;

namespace Azure2Java.QPCRAdapter {

    /**
     * @author zy gws ssc
     * @date 18.12.22
     * @update 19.12.06
     * @decription 仪器通讯操作工具类【Azure.QPCR.QPCR】
     */
    public class QpcrUtils {
        private QPCR _QPCR;

        // lazy loading
        public void init() {
            _QPCR = new QPCR();
        }

        // 包含了所有搜寻到的设备
        public ConnectedDeviceDictionary[] getConnectedDevices() {
            List<ConnectedDeviceDictionary> connectedDevices = new List<ConnectedDeviceDictionary>();
            foreach(var item in _QPCR.ConnectedDevices) {
                connectedDevices.Add(new ConnectedDeviceDictionary(item.Key, item.Value));
            }
            return connectedDevices.ToArray();
        }

        // 搜索仪器
        public DeviceInfoAdapter[] connections() {
            List<DeviceInfo> devices = _QPCR.Connections();
            int count = devices.Count;
            DeviceInfoAdapter[] result = new DeviceInfoAdapter[count];
            for(int i = 0; i < count; i++) {
                result[i] = new DeviceInfoAdapter(devices[i]);
            }
            return result;
        }

        // 连接 QPCR 函数，在操作 QPCR 前需使用此函数
        public bool connect(DeviceInfoAdapter deviceInfo) {
            return _QPCR.Connect(deviceInfo.DeviceInfo);
        }

        // 在不需要操作设备时，使用此函数断开与设备的连接
        public void disconnect(DeviceInfoAdapter deviceInfo) {
            _QPCR.Disconnect(deviceInfo.DeviceInfo);
        }

        public bool createWorkspace(DeviceInfoAdapter deviceInfoAdapter, string workspaceName) {
            return _QPCR.CreateWorkspace(deviceInfoAdapter.DeviceInfo, workspaceName);
        }

        public bool deleteWorkspace(DeviceInfoAdapter deviceInfoAdapter, string workspaceName) {
            return _QPCR.DeleteWorkspace(deviceInfoAdapter.DeviceInfo, workspaceName);
        }

        public ProgramBean getProgram(DeviceInfoAdapter deviceInfoAdapter, string workspaceName, string programName) {
            Program program = _QPCR.GetProgram(deviceInfoAdapter.DeviceInfo, workspaceName, programName);
            ProgramBean programAdapter = new ProgramBean(program);
            return programAdapter;
        }

        // 将 protocol 程序下载到相关设备
        public bool downloadProgram(DeviceInfoAdapter deviceInfo, ProgramBean program) {
            return _QPCR.DownloadProgram(deviceInfo.DeviceInfo, program.Program);
        }

        public bool deleteProgram(DeviceInfoAdapter deviceInfoAdapter, string workspaceName, string programName) {
            return _QPCR.DeleteProgram(deviceInfoAdapter.DeviceInfo, workspaceName, programName);
        }

        // 在程序运行前需设置本次实验的数据名称
        public bool setExperimentDataFileName(DeviceInfoAdapter deviceInfo, string workspaceName, string programName, string name) {
            return _QPCR.SetRunningExperimentDataFileName(deviceInfo.DeviceInfo, workspaceName, programName, name);
        }

        // 程序启动命令控制函数，在下载程序后，使用此命令启动相应设备的protocol程序
        public bool runProgram(DeviceInfoAdapter deviceInfo) {
            return _QPCR.RunProgram(deviceInfo.DeviceInfo);
        }

        // 暂停实验
        public bool pauseProgram(DeviceInfoAdapter deviceInfo) {
            return _QPCR.PauseProgram(deviceInfo.DeviceInfo);
        }

        // 恢复相关设备的protocol程序
        public bool resumeProgram(DeviceInfoAdapter deviceInfo) {
            return _QPCR.ResumeProgram(deviceInfo.DeviceInfo);
        }

        // 停止相关设备的protocol程序
        public bool stopProgram(DeviceInfoAdapter deviceInfo) {
            return _QPCR.StopProgram(deviceInfo.DeviceInfo);
        }

        // 跳过相关设备的protocol程序步骤
        public bool skipProgramStep(DeviceInfoAdapter deviceInfo) {
            return _QPCR.SkipProgramStep(deviceInfo.DeviceInfo);
        }

        public bool getWorkspaceSummary(DeviceInfoAdapter deviceInfoAdapter) {
            return _QPCR.GetWorkspaceSummary(deviceInfoAdapter.DeviceInfo);
        }

        // 获取相关设备的所有实验数据目录
        public bool getExperimentDataSummary(DeviceInfoAdapter deviceInfo) {
            return _QPCR.GetExperimentDataSummary(deviceInfo.DeviceInfo);
        }

        //获取相关设备的实验数据
        public bool getExperimentDataFiles(DeviceInfoAdapter deviceInfoAdapter, ExperimentDataInfoAdapter[] experimentDataInfoAdapters, string path) {
            List<ExperimentDataInfo> experimentDataInfos = new List<ExperimentDataInfo>();
            for(int i = 0; i < experimentDataInfoAdapters.Length; i++) {
                experimentDataInfos.Add(experimentDataInfoAdapters[i].ExperimentDataInfo);
            }
            return _QPCR.GetExperimentDataFiles(deviceInfoAdapter.DeviceInfo, experimentDataInfos, path);
        }

        //删除相关设备的实验数据
        public bool deleteExperimentDataFiles(DeviceInfoAdapter deviceInfoAdapter, ExperimentDataInfoAdapter[] experimentDataInfoAdapters) {
            List<ExperimentDataInfo> experimentDatas = new List<ExperimentDataInfo>();
            for(int i = 0; i < experimentDataInfoAdapters.Length; i++) {
                experimentDatas.Add(experimentDataInfoAdapters[i].ExperimentDataInfo);
            }
            return _QPCR.DeleteExperimentDataFiles(deviceInfoAdapter.DeviceInfo, experimentDatas);
        }

        // 第四版通讯库新增方法
        public bool getDyeSummary(DeviceInfoAdapter deviceInfoAdapter) {
            return _QPCR.GetDyeSummary(deviceInfoAdapter.DeviceInfo);
        }

        public ExperimentDataInfoAdapter getRunningExperimentInfos(DeviceInfoAdapter deviceInfoAdapter) {
            return new ExperimentDataInfoAdapter(_QPCR.GetRunningExperimentInfos(deviceInfoAdapter.DeviceInfo));
        }
    }
}
